package com.gitee.cirnochat.chatai.controller;

import com.gitee.cirnochat.chatai.model.vo.req.aimsg.AiChatMessageDTO;
import com.gitee.cirnochat.chatai.model.vo.req.aisession.AISessionCreateDTO;
import com.gitee.cirnochat.chatai.model.vo.req.aisession.AISessionUpdateDTO;
import com.gitee.cirnochat.chatai.model.vo.req.aisummary.AISummaryTextDTO;
import com.gitee.cirnochat.chatai.model.vo.resp.AIChatSessionVo;
import com.gitee.cirnochat.chatai.model.vo.resp.AiChatSessionMessageVo;
import com.gitee.cirnochat.chatai.service.AiChatService;
import com.gitee.cirnochat.common.model.vo.resp.PageVo;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.common.utils.RequestUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.List;

@Tag(name = "AI聊天接口")
@RestController
@RequestMapping("/ai")
@RequiredArgsConstructor
@Validated
public class AiChatController {

    private final AiChatService aiChatService;

    private final static long SSE_TIMEOUT = 5 * 60 * 1000L;

    //region 会话相关

    @Operation(summary = "创建会话")
    @PostMapping("/session")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<AIChatSessionVo> createSession(@RequestBody AISessionCreateDTO dto, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(aiChatService.createSession(dto, userId));
    }

    @Operation(summary = "获取会话列表")
    @GetMapping("/session/list")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<PageVo<AIChatSessionVo>> getSessionList(@Parameter(description = "搜索文本", example = "1") @RequestParam(required = false) String searchName,
                                                          @Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1", required = false) int page,
                                                          @Parameter(description = "每页数量", example = "10") @RequestParam(defaultValue = "20", required = false) int size,
                                                          HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(aiChatService.getSessionList(searchName, page, size, userId));
    }

    @Operation(summary = "删除会话")
    @DeleteMapping("/session/{id}")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<Boolean> deleteSession(@PathVariable Long id, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        aiChatService.deleteSession(id, userId);
        return Result.success();
    }


    @Operation(summary = "更新会话")
    @PutMapping("/session")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<AIChatSessionVo> updateSession(@RequestBody AISessionUpdateDTO dto, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(aiChatService.updateSession(dto, userId));
    }

    //endregion


    //region 消息相关

    @Operation(summary = "获取会话消息列表")
    @GetMapping("/chat/messages/{sessionId}")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<List<AiChatSessionMessageVo>> getMessagesBySessionId(@PathVariable Long sessionId , HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(aiChatService.getMessagesBySessionId(sessionId,userId));
    }

    @Operation(summary = "发送消息给AI")
    @PostMapping("/chat")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public SseEmitter sendUserChatMessage(@RequestBody @Valid AiChatMessageDTO dto , HttpServletRequest request) {
        SseEmitter sseEmitter = new SseEmitter(SSE_TIMEOUT);
        Long userId = RequestUtils.getUserId(request);
        aiChatService.completeOutput(sseEmitter,dto,userId);
        return sseEmitter;
    }

    @Operation(summary = "总结文本内容为一段不超过30字的文本句子")
    @PostMapping(path = "/summary/text")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<String> summaryTextContent(@RequestBody AISummaryTextDTO dto) {
       return Result.success(aiChatService.getSummaryFromText(dto));
    }

    //endregion

    //region 支持的模型相关
    @Operation(summary = "获取支持的模型列表")
    @GetMapping("/models")
    @Parameter(name = "Authorization", description = "认证信息", required = true , in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    public Result<List<String>> getSupportModels() {
        return Result.success(aiChatService.getSupportModels());
    }
    //endregion
}
